﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BenhVien
{
    public partial class Capthuoc : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private DataTable dt = new DataTable();
        Connect conn = new Connect();
        public Capthuoc()
        {
            InitializeComponent();
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from CapThuocBenhNhan";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        private void bt_NhapMoi_Click(object sender, EventArgs e)
        {
            txtMBN.Enabled = true;
            txtMT.Enabled = true;
            txtLL.Text = "";
            txtMBN.Text = "";
            txtMoTa.Text = "";
            txtSLTN.Text = "";
            txtTT.Text = "";
            txtCD.Text = "";
            txtMT.Text = "";
            d1.Text = "";
            d2.Text = "";
            bt_Luu.Enabled = false;
            bt_Xoa.Enabled = false;
        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            bt_Luu.Enabled = true;
            bt_Xoa.Enabled = true;
            txtMBN.Enabled = false;
            txtMT.Enabled = false;
            dt.Clear();
            getdata();
        }
        public void binding()
        {
            txtLL.DataBindings.Clear();
            txtLL.DataBindings.Add("Text", dataGridView1.DataSource, "LieuLuong");
            txtMBN.DataBindings.Clear();
            txtMBN.DataBindings.Add("Text", dataGridView1.DataSource, "MaBN");
            txtMoTa.DataBindings.Clear();
            txtMoTa.DataBindings.Add("Text", dataGridView1.DataSource, "MoTa");
            txtMT.DataBindings.Clear();
            txtMT.DataBindings.Add("Text", dataGridView1.DataSource, "MaThuoc");
            txtSLTN.DataBindings.Clear();
            txtSLTN.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuongTrongNgay");
            txtTT.DataBindings.Clear();
            txtTT.DataBindings.Add("Text", dataGridView1.DataSource, "TenThuoc");
            txtCD.DataBindings.Clear();
            txtCD.DataBindings.Add("Text", dataGridView1.DataSource, "CachDung");
            d1.DataBindings.Clear();
            d1.DataBindings.Add("Text", dataGridView1.DataSource, "NgayBatDau");
            d2.DataBindings.Clear();
            d2.DataBindings.Add("Text", dataGridView1.DataSource, "NgayKetThuc");
        }
        private void Capthuoc_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
            binding();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMBN.Text == "" | txtCD.Text == "" | txtLL.Text == "" | txtMoTa.Text == "" | txtMT.Text == "" | txtSLTN.Text == "" | txtTT.Text == "" | d1.Text == "" | d2.Text == "")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                if (check("Select MaBN from BenhNhan where MaBN =@DoiSo", txtMBN.Text) == false)
                    MessageBox.Show("Khong co Ma benh nhan", "Loi", MessageBoxButtons.OK);
                else
                {
                    if (check("select MaThuoc from Thuoc where MaThuoc = @DoiSo ", txtMT.Text) == false)
                        MessageBox.Show("Khong co Ma Thuoc", "lỗi", MessageBoxButtons.OK);
                    else
                    {
                        if (check("select MaThuoc from CapThuocBenhNhan where MaThuoc = @DoiSo ", txtMT.Text) == true)
                            MessageBox.Show("Ma Thuoc da co", "lỗi", MessageBoxButtons.OK);
                        else
                        {
                            DataRow row = dt.NewRow();
                            row["MaBN"] = txtMBN.Text;
                            row["MaThuoc"] = txtMT.Text;
                            row["SoLuongTrongNgay"] = txtSLTN.Text;
                            row["NgayBatDau"] = d1.Value;
                            row["NgayKetThuc"] = d2.Value;
                            row["TenThuoc"] = txtTT.Text;
                            row["MoTa"] = txtMoTa.Text;
                            row["CachDung"] = txtCD.Text;
                            row["LieuLuong"] = txtLL.Text;

                            dt.Rows.Add(row);
                            SqlCommand commandInsert = new SqlCommand();
                            commandInsert.Connection = conn.con;
                            commandInsert.CommandType = CommandType.Text;
                            commandInsert.CommandText = @"Insert CapThuocBenhNhan (MaBN, MaThuoc, SoLuongTrongNgay, NgayBatDau, NgayKetThuc, TenThuoc, MoTa, CachDung, LieuLuong) Values 
                                                ( @MaBN, @MaThuoc, @SoLuongTrongNgay, @NgayBatDau, @NgayKetThuc, @TenThuoc, @MoTa, @CachDung, @LieuLuong)";
                            commandInsert.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
                            commandInsert.Parameters.Add("@MaThuoc", SqlDbType.NVarChar, 50, "MaThuoc");
                            commandInsert.Parameters.Add("@SoLuongTrongNgay", SqlDbType.Int, 10, "SoLuongTrongNgay");
                            commandInsert.Parameters.Add("@NgayBatDau", SqlDbType.Date, 50, "NgayBatDau");
                            commandInsert.Parameters.Add("@NgayKetThuc", SqlDbType.Date, 50, "NgayKetThuc");
                            commandInsert.Parameters.Add("@TenThuoc", SqlDbType.NVarChar, 50, "TenThuoc");
                            commandInsert.Parameters.Add("@MoTa", SqlDbType.NVarChar, 50, "MoTa");
                            commandInsert.Parameters.Add("@CachDung", SqlDbType.NVarChar, 50, "CachDung");
                            commandInsert.Parameters.Add("@LieuLuong", SqlDbType.NVarChar, 50, "LieuLuong");
                            da.InsertCommand = commandInsert;
                            da.Update(dt);
                            MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                        }
                    }
                }
            }
        }
        private bool check(String s, String s1)
        {
            bool check = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check = true;
            return check;
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN like '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = conn.con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"Delete from CapThuocBenhNhan Where MaBN=@MaBN";
            commandDelete.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("ban da xóa thanh cong", "chuc mung", MessageBoxButtons.OK);
        }

        private void bt_Luu_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaBN like '" + txtMBN.Text + "'")[0];
            row.BeginEdit();
            row["MaBN"] = txtMBN.Text;
            row["MaThuoc"] = txtMT.Text;
            row["SoLuongTrongNgay"] = txtSLTN.Text;
            row["NgayBatDau"] = d1.Value;
            row["NgayKetThuc"] = d2.Value;
            row["TenThuoc"] = txtTT.Text;
            row["MoTa"] = txtMoTa.Text;
            row["CachDung"] = txtCD.Text;
            row["LieuLuong"] = txtLL.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update CapThuocBenhNhan Set  MaThuoc=@MaThuoc, SoLuongTrongNgay=@SoLuongTrongNgay, NgayBatDau=@NgayBatDau, NgayKetThuc=@NgayKetThuc, TenThuoc=@TenThuoc, MoTa=@MoTa, CachDung=@CachDung, LieuLuong=@LieuLuong
                                                              where MaBN= @MaBN";
            commandUpdate.Parameters.Add("@MaBN", SqlDbType.NVarChar, 50, "MaBN");
            commandUpdate.Parameters.Add("@MaThuoc", SqlDbType.NVarChar, 50, "MaThuoc");
            commandUpdate.Parameters.Add("@SoLuongTrongNgay", SqlDbType.Int, 10, "SoLuongTrongNgay");
            commandUpdate.Parameters.Add("@NgayBatDau", SqlDbType.Date, 50, "NgayBatDau");
            commandUpdate.Parameters.Add("@NgayKetThuc", SqlDbType.Date, 50, "NgayKetThuc");
            commandUpdate.Parameters.Add("@TenThuoc", SqlDbType.NVarChar, 50, "TenThuoc");
            commandUpdate.Parameters.Add("@MoTa", SqlDbType.NVarChar, 50, "MoTa");
            commandUpdate.Parameters.Add("@CachDung", SqlDbType.NVarChar, 50, "CachDung");
            commandUpdate.Parameters.Add("@LieuLuong", SqlDbType.NVarChar, 50, "LieuLuong");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        }
    }
}
